package com.wonders.framework.auto.builder.code.convert.instance;

import com.wonders.framework.auto.builder.code.convert.BaseTagConvert;
import com.wonders.framework.auto.builder.code.entity.vo.BuildColumn;
import com.wonders.framework.auto.builder.code.entity.vo.BuildTable;

/**
 * 数据查询标签转换器
 * 
 * @author WuBin
 * 
 */
public class DaoQueryConvert extends BaseTagConvert {

	@Override
	public String convert(String str, String tag, BuildTable buildTable) {
		String prefix = super.getPrefix(str, tag);
		str = "";
		for (int i = 0; i < buildTable.getColumns().size(); i++) {
			BuildColumn buildColumn = buildTable.getColumns().get(i);
			if ("Date".equals(buildColumn.getTypeName())) {
				str += prefix + "if(" + buildTable.getInstanceName() + "Vo.get"
						+ buildColumn.getFunctionName() + "() != null){";
				str += "\n";
				str += prefix + "    criteria.add(Restrictions.between(\""
						+ buildColumn.getPropertyName() + "\", "
						+ buildTable.getInstanceName() + "Vo.get"
						+ buildColumn.getFunctionName()
						+ "(), DateUtil.getDateByDays("
						+ buildTable.getInstanceName() + "Vo.get"
						+ buildColumn.getFunctionName() + "(), 1)));";
				str += "\n";
				str += prefix + "}";
			} else if ("String".equals(buildColumn.getTypeName())) {
				str += prefix + "if(StringUtils.isNotBlank("
						+ buildTable.getInstanceName() + "Vo.get"
						+ buildColumn.getFunctionName() + "())){";
				str += "\n";
				str += prefix + "    criteria.add(Restrictions.like(\""
						+ buildColumn.getPropertyName() + "\", \"%\" + "
						+ buildTable.getInstanceName() + "Vo.get"
						+ buildColumn.getFunctionName() + "() + \"%\"));";
				str += "\n";
				str += prefix + "}";
			} else if ("Long".equals(buildColumn.getTypeName())
					|| "Integer".equals(buildColumn.getTypeName())) {
				str += prefix + "if(" + buildTable.getInstanceName() + "Vo.get"
						+ buildColumn.getFunctionName() + "() != null){";
				str += "\n";
				str += prefix + "    criteria.add(Restrictions.eq(\""
						+ buildColumn.getPropertyName() + "\", "
						+ buildTable.getInstanceName() + "Vo.get"
						+ buildColumn.getFunctionName() + "()));";
				str += "\n";
				str += prefix + "}";
			}
			if (i + 1 != buildTable.getColumns().size()) {
				str += "\n";
			}
		}
		return str;
	}

}
